// components/search/search.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    blogsList: {
      type: Array,
    },
    fixed: {
      type: Boolean
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    handleSearch: false,
    searchIpt: '',
    placeholder: '请输入关键词搜索'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleInput(e) {
      const { value } = e.detail;
      this.setData({
        searchIpt: value
      })

      if (value != '') {
        this.setData({
          handleSearch: false
        })
      }
    },
    searchBlogs() {
      const that = this;
      let { blogsList } = that.properties;
      const { searchIpt } = that.data;
      if (searchIpt == '') {
        return
      };
      that.setData({
        placeholder: searchIpt,
      }, () => {
        const searchBlogsList = blogsList.filter(i => i.title.indexOf(searchIpt) >= 0);
        // console.log(searchBlogsList);
        that.setData({
          searchIpt: '',
          handleSearch: true
        });
        that.triggerEvent('onSearch', { list: searchBlogsList, search: true });
      })
    },
    cancelSearch() {
      const that = this;
      let { blogsList } = that.properties;
      that.setData({
        searchIpt: '',
        placeholder: '请输入关键词搜索',
        handleSearch: false
      });
      that.triggerEvent('onSearch', { list: [], search: false });
    }
  }
})